import { AudioType, BtnColor, CardType, ListenEventType } from "../../util/defineConfig";
import View from "../../util/View";
import gamePanel from "../gamePanel/gamePanel";

const {ccclass, property} = cc._decorator;

@ccclass
export default class UIAnimation extends View {

    private animationState:cc.AnimationState = null
    private type:CardType = null
    private time:number = null
    private color:BtnColor = null
  init(gamePanel:gamePanel,...args:any){
    this.type = args[0]
    let animation = this.node.getComponent(cc.Animation)
    this.animationState = animation.getAnimationState(args[1])
    if(args[0] === CardType.changeColor){
      this.color = Math.floor(Math.random()*4) + 1
      this.time = 2 +  this.color* (1 / 4)
    }
  }

  update(dt){
      if(!this.animationState.isPlaying && this.type !== CardType.changeColor){
          this.emit(ListenEventType.ANIMATION_OVER,this.type)
          this.node.destroy()
      }
      if(this.type === CardType.changeColor){
        this.time -= dt
        if(this.time<=0){
          this.time = 999
          this.animationState.pause()
          this.emit(ListenEventType.PAUSE_EFFECT,AudioType.change_color)
          this.scheduleOnce(()=>{
          this.emit(ListenEventType.ANIMATION_OVER,this.type,this.color)
            this.node.destroy()
          },1)
        }
      }
  }
}
